enchant.strokeCats = { assets: ['cat.gif','font2.png'] };

enchant.strokeCats.Point = enchant.Class.create({
    initialize: function(x, y) {
        this.x = x;
        this.y = y;
    }
});

enchant.strokeCats.State = enchant.Class.create({});
enchant.strokeCats.State.WALK = 'walk';
enchant.strokeCats.State.SLEEP = 'sleep';
enchant.strokeCats.State.STOP = 'stop';
enchant.strokeCats.State.RUN = 'run';
enchant.strokeCats.State.WANT = 'want';
enchant.strokeCats.State.AWAY = 'away';
enchant.strokeCats.State.EAT = 'eat';

enchant.strokeCats.Level = enchant.Class.create({});
enchant.strokeCats.Level.SLEEP = [240, 210, 180, 160, 140, 120, 110, 100, 90, 80];
enchant.strokeCats.Level.STOP = [150, 130, 110, 100, 90, 80, 75, 70, 65, 60];
enchant.strokeCats.Level.WANT = [150, 130, 110, 100, 90, 80, 75, 70, 65, 60];
enchant.strokeCats.Level.AWAY = [180, 210, 240, 270, 300, 330, 360, 390, 420, 450];
enchant.strokeCats.Level.WALK_FRAME = [6, 6, 5, 5, 4, 4, 3, 3, 2, 2];
enchant.strokeCats.Level.WALK_DIST = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5];

enchant.strokeCats.Cat = enchant.Class.create(enchant.Sprite, {
    
	initialize: function() {
		var game = enchant.Game.instance;
		var image = game.assets['cat.gif'];
		enchant.Sprite.call(this, 32, 32);

        this.dx = 1;
        this.dy = 0;
        this.frame_Sleep = 0;
        this.frame_Stop = 0;
        this.frame_Want = 0;
        this.frame_Away = 0;
        this.frame_Eat = 0;
        
        this.interval_Sleep = 240; //8 seconds
        this.interval_Stop = 150;   //5 seconds
        this.interval_Want = 150;  //5 seconds
        this.interval_Away = 240;  //8 seconds
        this.interval_Eat = 60;   //2 seconds
        
        this.walk_frame = 6;
        this.walk_dist = 1;
                
    	this.image = image;
        this._state = enchant.strokeCats.State.WALK;
        this._goal = null;
        this._level = 0;

		//event handle touch event
		this.addEventListener(enchant.Event.TOUCH_START, function(e){
            var game = enchant.Game.instance;
            e.type = 'mousedown';
            e.dispatcher = this;  //target ???
            game.dispatchEvent(e);
		});
        this.addEventListener(enchant.Event.ENTER_FRAME, function(e){
            var game = enchant.Game.instance;
                        
            switch(this._state){
                case enchant.strokeCats.State.WALK:
                this.x += this.dx;
                if(Math.abs(this.x - this.goal.x) < 5){
                    //want or stop
                    var p = Math.random();
                    if(p < 0.25){ // 25%
                        this.state = enchant.strokeCats.State.WANT;
                    }else{ //75%
                        this.state = enchant.strokeCats.State.STOP;    
                    }
                    return;
                }
                
                //out of game space
                if(this.x < -this.width || this.x > game.width
                || this.y < -this.height || this.y > game.height){
                    this.state = enchant.strokeCats.State.AWAY;
                    var event = new Event('change');
                    event.dispatcher = this;
                    game.dispatchEvent(event);
                    break;
                }                
                
                //framerate
                if(game.frame % this.walk_frame == 0){                
                    this.frame++;
                    if(this.dx < 0){
                        this.frame %= 3;
                    }
                    else{
                        this.frame %= 3;
                        this.frame += 3;
                    }
                }
                break;
                
                case enchant.strokeCats.State.SLEEP:
                this.frame_Sleep++;
                if(this.frame_Sleep > this.interval_Sleep){ //5 seconds
                    this.state = enchant.strokeCats.State.WALK;
                    var event = new Event('change');
                    event.dispatcher = this;
                    game.dispatchEvent(event);                    
                 }
                
                //framerate
                if(game.frame % 6 == 0){
                    this.frame++;
                    if(this.dx < 0){
                        this.frame %= 3;
                        this.frame += 6;
                    }
                    else{
                        this.frame %= 3;
                        this.frame += 9;
                    }
                }                
                break;
                
                case enchant.strokeCats.State.STOP:
                this.frame_Stop++;
                if(this.frame_Stop > this.interval_Stop){
                    this.state = enchant.strokeCats.State.RUN;
                    var event = new Event('change');
                    event.dispatcher = this;
                    game.dispatchEvent(event);
                    break;
                }
                
                //framerate
                
                if(game.frame % 6 == 0){                
                    this.frame++;
                    if(this.dx < 0){
                        this.frame %= 3;
                        this.frame += 30;
                    }
                    else{
                        this.frame %= 3;
                        this.frame += 33;
                    }
                }
                                
                break;
                
                case enchant.strokeCats.State.WANT:
                this.frame_Want++;
                if(this.frame_Want > this.interval_Want){
                    this.state = enchant.strokeCats.State.RUN;
                    var event = new Event('change');
                    event.dispatcher = this;
                    game.dispatchEvent(event);
                    break;
                }

                //framerate
                if(game.frame % 6 == 0){
                    this.frame++;
                    if(this.dx < 0){
                        this.frame %= 3;
                        this.frame += 12;
                    }
                    else{
                        this.frame %= 3;
                        this.frame += 15;
                    }
                }
                break;
                
                case enchant.strokeCats.State.RUN:
                this.x += this.dx;
                
                //out of game space
                if(this.x < -this.width || this.x > game.width
                || this.y < -this.height || this.y > game.height){
                    this.state = enchant.strokeCats.State.AWAY;
                    var event = new Event('change');
                    event.dispatcher = this;
                    game.dispatchEvent(event);
                    break;
                }
                
                //framerate
                if(game.frame % 2 == 0){                
                    this.frame++;
                    if(this.dx < 0){
                        this.frame %= 3;
                    }
                    else{
                        this.frame %= 3;
                        this.frame += 3;
                    }
                }
                break;
                case enchant.strokeCats.State.AWAY:
                this.frame_Away++;
                if(this.frame_Away > this.interval_Away){
                    this.state = enchant.strokeCats.State.WALK;
                    var event = new Event('change');
                    event.dispatcher = this;
                    game.dispatchEvent(event);                  
                }
                break;
                
                case enchant.strokeCats.State.EAT:
                this.frame_Eat++;
                if(this.frame_Eat > this.interval_Eat){
                    this.state = enchant.strokeCats.State.SLEEP;
                    var event = new Event('change');
                    event.dispatcher = this;
                    game.dispatchEvent(event);                    
                }
                if(game.frame % 20 == 0){
                    this.frame++;
                    if(this.dx < 0){
                        this.frame %= 3;
                        this.frame += 24;
                    }
                    else{
                        this.frame %= 3;
                        this.frame += 27;
                    }
                }
                break;             
            }
            
        })
	},

    goal: {
        get: function(){
            return this._goal;
        },
        set: function(point){    
            console.log("setGoal " + point.x + " " + point.y);
            this._goal = point;
        
            //walk
            if(this.state == 'walk'){
                if((this.x - this.goal.x) > 0){
                    this.dx = -this.walk_dist;
                }else{
                    this.dx = this.walk_dist;
                }
            }
            else if(this._state == 'run'){
                if((this.x - this.goal.x) > 0){
                    this.dx = -(this.walk_dist + 3);
                }else{
                    this.dx = this.walk_dist + 3;
                }
            }
        }      
    },
    state: {
        get: function() {
            return this._state;
        },
        set: function(state) {
            this._state = state;
            this.frame_Sleep = 0;
            this.frame_Stop = 0;
            this.frame_Want = 0;
            this.frame_Away = 0;
            this.frame_Eat = 0;
        } 
    },
    level: {
        get: function() {
            return this._level;
        },
        set: function(level) {
            this._level = level;
            this.interval_Sleep = enchant.strokeCats.Level.SLEEP[level];
            this.interval_Stop = enchant.strokeCats.Level.STOP[level];
            this.interval_Want = enchant.strokeCats.Level.WANT[level];
            this.interval_Away = enchant.strokeCats.Level.AWAY[level];            
            this.walk_frame = enchant.strokeCats.Level.WALK_FRAME[level];
            this.walk_dist =  enchant.strokeCats.Level.WALK_DIST[level];
        }
    }
});

enchant.strokeCats.Hand = enchant.Class.create(enchant.Sprite, {
	initialize: function() {
        console.log("initialize");
    
		var game = enchant.Game.instance;
		var image = game.assets['cat.gif'];
		enchant.Sprite.call(this, 32, 32);

        this.count = 0;
        this.image = image;
        this.visible = false;
    },

    paint: function(x, y) {
		var game = enchant.Game.instance;
        this.count = 0;
        this.x = x;
        this.y = y;
        this.frame = 21;
        
        var enterframeListener = function(e){
            //framerate
            if(game.frame % 6 == 0){
                this.visible = true;
                this.frame++;
                this.frame %= 3;
                this.frame += 21;                
                this.count++;
                if(this.count > 6){
                    this.visible = false;
                    this.removeEventListener(enchant.Event.ENTER_FRAME, enterframeListener);
                }
            }        
        };
        this.addEventListener(enchant.Event.ENTER_FRAME, enterframeListener);
    }
});

enchant.strokeCats.Fish = enchant.Class.create(enchant.Sprite, {
	initialize: function() {
        console.log("initialize");
    
		var game = enchant.Game.instance;
		var image = game.assets['cat.gif'];
		enchant.Sprite.call(this, 32, 32);
        
        this.image = image;
        this.x = game.width / 2.0 - this.width / 2.0;
        this.y = game.height - this.height;
        this.frame = 19;
        this.visible = true;
        this.dx = 0;
        this.dy = 0;
        
        this.addEventListener(enchant.Event.TOUCH_START, function(e){
            this.addEventListener(enchant.Event.TOUCH_MOVE, this.touchmoveListener);
            this.addEventListener(enchant.Event.TOUCH_END, this.touchendListener);
        });
    },
    reset: function() {
        console.log("reset");
		var game = enchant.Game.instance;
        this.x = game.width / 2.0 - this.width / 2.0;
        this.y = game.height - this.height;
        this.dy = 0;
        this.removeEventListener(enchant.Event.TOUCH_MOVE, this.touchmoveListener);
        this.removeEventListener(enchant.Event.TOUCH_END, this.touchendListener);        
    },
    touchmoveListener: function(e){
        console.log("touchmove");
        var game = enchant.Game.instance;
        this.x = e.x - this.width / 2.0;
        this.y = e.y - this.height / 2.0;
            
        e.type = 'mousedown';
        e.dispatcher = this;  //target ???
        game.dispatchEvent(e);
    },
    touchendListener: function(e){
        console.log("touchend");
        this.addEventListener(enchant.Event.ENTER_FRAME, this.enterframeListener);
    },
    enterframeListener: function(e){
        console.log("enterframe");
        var game = enchant.Game.instance;

        //free fall
        this.y += this.dy;
        this.dy++;
        if(this.y > game.height){
            this.removeEventListener(enchant.Event.ENTER_FRAME, this.enterframeListener);
            this.reset();
        }    
    }
});

/**
 * Score class
 **/
enchant.strokeCats.Score = enchant.Class.create(enchant.Group, {
	initialize: function() {
        console.log("initialize");
		enchant.Group.call(this);
    
		var game = enchant.Game.instance;
		var image = game.assets['font2.png'];
        
        this.ope = new Sprite(16, 16);
        this.ope.image = image;
        this.ope.frame = 11;
        this.ope.visible = false;
        this.addChild(this.ope);
        
        this.num = new Sprite(16, 16);
        this.num.image = image;
        this.num.frame = 16;
        this.num.x = 10;
        this.num.visible = false;
        this.addChild(this.num);

        this.dx = 0;
        this.dy = 0;        
        this.count = 0;
    },
    paint: function(x, y, number) {
		var game = enchant.Game.instance;
    
        this.x = x;
        this.y = y;
        this.dy = -1;
        this.count = 0;
        
        if(number > 0){
            this.ope.frame = 11;
            this.num.frame = 16 + number;
        }
        else if(number < 0){
            this.ope.frame = 13;
            this.num.frame = 16 - number;        
        }
        
        var enterframeListener = function(e){
            //framerate
            if(game.frame % 3 == 0){
                this.ope.visible = true;       
                this.num.visible = true;
                this.y += this.dy;
                this.count++;
                if(this.count > 6){
                    this.ope.visible = false;       
                    this.num.visible = false;
                    this.dy = 0;
                    this.removeEventListener(enchant.Event.ENTER_FRAME, enterframeListener);
                }
            }        
        };
        this.addEventListener(enchant.Event.ENTER_FRAME, enterframeListener);
    }    
});

/**
 * Combo class
 **/
enchant.strokeCats.Combo = enchant.Class.create(enchant.Group, {
	initialize: function() {
        console.log("initialize");
		enchant.Group.call(this);
    
		var game = enchant.Game.instance;
		var image = game.assets['font2.png'];

        var string = [35, 47, 45, 34, 47, 1]; //COMBO!
        this.sprites_combo = new Array(string.length);

        for(var i = 0, len = string.length; i < len; i++){
            this.sprites_combo[i] = new Sprite(16, 16);
            this.sprites_combo[i].image = image;
            this.sprites_combo[i].frame = string[i];
            this.sprites_combo[i].x = 12 * i;
            this.sprites_combo[i].y = 0;
            this.sprites_combo[i].visible = false;
            this.addChild(this.sprites_combo[i]);
        }
        
        this.tenth_digit = new Sprite(16, 16);
        this.tenth_digit.image = image;
        this.tenth_digit.frame = 16;
        this.tenth_digit.x = 24;
        this.tenth_digit.y = 16;
        this.tenth_digit.visible = false;
        this.addChild(this.tenth_digit);
        
        this.one_digit = new Sprite(16, 16);
        this.one_digit.image = image;
        this.one_digit.frame = 16;
        this.one_digit.x = 36;
        this.one_digit.y = 16;
        this.one_digit.visible = false;
        this.addChild(this.one_digit);

        this.dx = 0;
        this.dy = 0;        
        this.count = 0;
    },
    paint: function(x, y, number) {
		var game = enchant.Game.instance;
    
        this.x = x;
        this.y = y;
        this.dy = -1;
        this.count = 0;
        
        if(number < 100){
            var tenth_num = Math.floor(number / 10);
            var one_num = number % 10;
            if(tenth_num > 0){
                this.tenth_digit.frame = 16 + tenth_num;
            }
            else{
                this.tenth_digit.frame = 0;
            }
            this.one_digit.frame = 16 + one_num;       
        }
        else{
            this.tenth_digit.frame = 0;
            this.one_digit.frame = 0;
        }
        
        var enterframeListener = function(e){
            //framerate
            if(game.frame % 3 == 0){
                this.one_digit.visible = true;       
                this.tenth_digit.visible = true;
                for(var i = 0, len = this.sprites_combo.length; i < len; i++){
                    this.sprites_combo[i].visible = true;
                }
                this.y += this.dy;
                this.count++;
                if(this.count > 6){
                    this.one_digit.visible = false;       
                    this.tenth_digit.visible = false;
                    for(var i = 0, len = this.sprites_combo.length; i < len; i++){
                        this.sprites_combo[i].visible = false;
                    }
                    this.dy = 0;
                    this.removeEventListener(enchant.Event.ENTER_FRAME, enterframeListener);
                }
            }        
        };
        this.addEventListener(enchant.Event.ENTER_FRAME, enterframeListener);
    }
});
